package com.atguigu.crowd.handler;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.po.MemberPo;
import com.atguigu.crowd.service.api.MemberService;
import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MemberProviderHandler {

    @Autowired
    private MemberService memberService;

    @RequestMapping("/save/member/remote")
    public ResultEntity<String> saveMember(@RequestBody MemberPo memberPo){

        try {
            memberService.saveMember(memberPo);

            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException){
                return ResultEntity.failed(CrowdConstant.MESSAGE_LOGIN_ACC_ALREADY_IN_USE);
            }
            return ResultEntity.failed(e.getMessage());
        }

    }


     @RequestMapping("/get/memberpo/by/login/acct/remote")
     ResultEntity<MemberPo> getMemberByLoginAcctRemote(@RequestParam("loginacct")String loginacct){

         try {
             //1.调用本地service查询
             MemberPo memberPo = memberService.getMemberPOByLoginAcct(loginacct);

             return ResultEntity.successWithData(memberPo);

         } catch (Exception e) {
             e.printStackTrace();

             return ResultEntity.failed(e.getMessage());
         }
     }
}
